const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';

Page({
  data: {
    avatarUrl: defaultAvatarUrl,  // 用于显示的头像
    nickname: '',
    openid: '',
    modalName: null,
    tempAvatarUrl: '',  // 临时存储用户选择的头像
  },

  onShow() {
    // 检查是否已登录
    const userProfile = wx.getStorageSync('userProfile');
    const openid = wx.getStorageSync('openid');
    if (userProfile && openid) {
      this.setData({
        avatarUrl: userProfile.avatarUrl,
        nickname: userProfile.nickname,
        openid: openid
      });
    }
  },

  // 选择头像，不立即更新主页面的头像
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    this.setData({
      tempAvatarUrl: avatarUrl  // 暂时存储用户选择的头像
    });
  },

  // 表单提交事件
// 表单提交事件
onFormSubmit(e) {
  const { nickname } = e.detail.value;   
  const { tempAvatarUrl } = this.data;   
  const openid = wx.getStorageSync('openid');  

  if (!nickname) {
    wx.showToast({
      title: '请先选择昵称',
      icon: 'none'
    });
    return;
  }

  if (!tempAvatarUrl) {
    wx.showToast({
      title: '请先选择头像',
      icon: 'none'
    });
    return;
  }

  wx.cloud.callFunction({
    name: 'get_openid', 
    data: {
      avatarUrl: tempAvatarUrl,
      nickname,
      openid
    },
    success: (cloudRes) => {
      const openid = cloudRes.result.openid;

      // 将头像和昵称保存到本地缓存
      wx.setStorageSync('userProfile', { avatarUrl: tempAvatarUrl, nickname });
      wx.setStorageSync('openid', openid);
      // 更新页面数据
      this.setData({
        avatarUrl: tempAvatarUrl,
        nickname: nickname,
        openid: openid
      });

      wx.showToast({
        title: '登录成功',
        icon: 'success'
      });

      this.hideModal(); // 关闭模态框
    },
    fail: (err) => {
      wx.showToast({
        title: '登录失败',
        icon: 'none'
      });
      console.error(err);
    }
  });
},

  record:function() {
    if (this.data.nickname && this.data.openid) {
      wx.navigateTo({
        url: './record/record?openid=' + this.data.openid, 
      })
    } else {
      wx.showToast({
        title: '请登录',
        icon: 'none'
      });

    }
  },

  // 显示模态框
  showModal(e) {
    // 如果用户已登录，提示已登录，不弹出模态框
    if (this.data.nickname && this.data.openid) {
      wx.showToast({
        title: '您已登录',
        icon: 'none'
      });
    } else {
      // 否则，弹出模态框
      this.setData({
        modalName: e.currentTarget.dataset.target
      });
    }
  },

  // 隐藏模态框
  hideModal(e) {
    this.setData({
      modalName: null
    });
  },
  logout() {
    wx.showModal({
      title: '确认退出登录',
      content: '您确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          // 清除本地缓存的用户信息
          wx.removeStorageSync('userProfile');
          wx.removeStorageSync('openid');

          // 重置页面数据
          this.setData({
            avatarUrl: defaultAvatarUrl,
            nickname: '',
            openid: ''
          });

          wx.showToast({
            title: '已退出登录',
            icon: 'success'
          });
        }
      }
    });
  },


  message:function(){
    wx.navigateTo({
      url: './persion_message/persion_message',
    })
  },
  gotoLog:function(){
    wx.navigateTo({
      url: './log/log',
    })
  },
  ToLove:function(){
    wx.navigateTo({
      url: './love/love',
    })
  }
});
